import { Http } from '../../utils/http'
import { Product } from '../../model/Product'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      list:[],
      gender:"",
      index:"",
      coupon_id:"", //领取跳进来选择的门店
      name:'',
      phone:'',
      radio: true,
      store_name:'',
      store_id:''
  },

  // 生命周期函数--监听页面加载
  onLoad: async function (options) {
    this.setData({
      index:options.index,
      coupon_id:options.coupon_id,
      name:options.username,
      phone:options.phone
    })
    app.globalData.username = options.username;
    app.globalData.phone = options.phone;

    let http = Http.login();
    let product = await Product.get_store_list();
    this.setData({
      list: product.data,
    })

  },

  //  选中radio
  onChange(event) {
    let name;
    let address;
    for (let index = 0; index < this.data.list.length; index++) {
      if(this.data.list[index].id == event.detail+1){
        name = this.data.list[index].name;
        address = this.data.list[index].address;
      }
    }
    this.setData({
      radio: event.detail,
      store_name:name,
      store_id:event.detail+1,
      address
    });
    this.navto();
  },

  navto:function(e){
    let that = this;
    // console.log( e.currentTarget.dataset['name']);
    if(that.data.index == 1){
      wx.redirectTo({//免费领取
        url: '/pages/getcoupon/index?store_id='+that.data.store_id+"&store_name="+that.data.store_name+"&coupon_id="+that.data.coupon_id+"&username="+that.data.name+"&phone="+that.data.phone+"&address="+that.data.address,
      })
    } 
    else if(that.data.index == 9){//授权管理 核销员
      wx.setStorageSync('storeId', this.data.store_id)
      wx.setStorageSync('storeName', this.data.store_name)
      wx.navigateBack ()
     
    } else if(that.data.index == 10){//授权管理 分销员
      wx.setStorageSync('storeId', this.data.store_id)
      wx.setStorageSync('storeName', this.data.store_name)
      wx.navigateBack ()
    }else{
      wx.redirectTo({//商品购买
        url: '/pages/pay/index?store_id='+that.data.store_id+"&store_name="+that.data.store_name+"&address="+that.data.address+"&mack=1",
      })
    }
  },

  onShow: function () {
    wx.hideHomeButton()
  }
})